package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.application.MyApplication;
import mamikeji.liaowa.bean.DiaryEveryDayBean;

/**
 * ExpandListView的适配器，继承自BaseExpandableListAdapter
 */
public class DiaryListExpandableListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private Context mContext;
    private List<DiaryEveryDayBean.DataEntity> list;
    OnItemClickListener mOnItemClickListener;
    public boolean bool_delete = false;
    User_EveryDay_DiaryAdapter user_diaryAdapter;

    public DiaryListExpandableListAdapter(Context context, List<DiaryEveryDayBean.DataEntity> list) {
        this.mContext = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onShareClick(int position, int groupPosition, int childPosition);

        void onDeleteClick(int diary_id, int position, int groupPosition, int childPosition);
    }

    /*
     *  Gets the data associated with the given child within the given group
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        //我们这里返回一下每个item的名称，以便单击item时显示
        return list.get(groupPosition).getList().get(childPosition);
    }


    /*
     * 取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID（分组及子项目的ID）
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /*
     *  Gets a View that displays the data for the given child within the given group
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        ChildHolder childHolder = null;
        View header_view = null;
        View foot_view = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_diary_childitem, parent, false);
            childHolder = new ChildHolder();
            childHolder.listView_diary_childitem_date = (ListView) convertView.findViewById(R.id.listView_diary_childitem_date);
            header_view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_user_diary_header, null);
            foot_view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_user_diary_foot, null);
            childHolder.header_diary_childitem_time = (TextView) header_view.findViewById(R.id.header_diary_childitem_time);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        List<DiaryEveryDayBean.DataEntity.ListEntity> listBeanList = getGroupItem(groupPosition).getList();

        if (childHolder.listView_diary_childitem_date.getHeaderViewsCount() == 0) {
            childHolder.listView_diary_childitem_date.addHeaderView(header_view);
        }

        if (childHolder.listView_diary_childitem_date.getFooterViewsCount() == 0) {
            childHolder.listView_diary_childitem_date.addFooterView(foot_view);
        }

        String[] mTimeSplit = getGroupItem(groupPosition).getDay().split("-");
        String time = mTimeSplit[1] + "-" + mTimeSplit[2];
        childHolder.header_diary_childitem_time.setText(time);
        user_diaryAdapter = new User_EveryDay_DiaryAdapter(listBeanList, mContext);

        bool_delete = user_diaryAdapter.bool_delete;

        childHolder.listView_diary_childitem_date.setAdapter(user_diaryAdapter);
        user_diaryAdapter.setList(listBeanList);
        user_diaryAdapter.setOnItemClickListener(new User_EveryDay_DiaryAdapter.OnItemClickListener() {
            @Override
            public void onShareClick(int position) {
                mOnItemClickListener.onShareClick(position,groupPosition,childPosition);
            }

            @Override
            public void onDeleteClick(int diary_id, int position) {
                mOnItemClickListener.onDeleteClick(diary_id , position , groupPosition,childPosition);
            }
        });
        return convertView;

    }

    public void getDelete(){
        user_diaryAdapter.bool_delete = bool_delete;
        user_diaryAdapter.notifyDataSetChanged();
    }

    /*
     * 取得指定分组的子元素数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    /**
     * 取得与给定分组关联的数据
     */
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    /**
     * 取得分组数
     */
    @Override
    public int getGroupCount() {
        return list.size();
    }

    /**
     * 取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID（分组及子项目的ID）
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_diary_groupitem, null);
            groupHolder = new GroupHolder();
            groupHolder.imageView_diary_no = (ImageView) convertView.findViewById(R.id.imageView_diary_no);
            groupHolder.textView_diary_name = (TextView) convertView.findViewById(R.id.textView_diary_name);
            groupHolder.textView_diary_num = (TextView) convertView.findViewById(R.id.textView_diary_num);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        final DiaryEveryDayBean.DataEntity groupBean = list.get(groupPosition);
        groupHolder.textView_diary_name.setText(groupBean.getDay());
        groupHolder.textView_diary_num.setText(groupBean.getList().size() + "");

        if(isExpanded){
            groupHolder.imageView_diary_no.setBackgroundResource(R.drawable.icon_rj_xiangxia_n);
        }else{
            groupHolder.imageView_diary_no.setBackgroundResource(R.drawable.icon_rj_qianjin_n); }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class GroupHolder {
        ImageView imageView_diary_no;
        TextView textView_diary_name;
        TextView textView_diary_num;
    }

    private class ChildHolder {
        ListView listView_diary_childitem_date;
        TextView header_diary_childitem_time;
    }

    public DiaryEveryDayBean.DataEntity getGroupItem(int position) {
        return list.get(position);
    }

    public List<DiaryEveryDayBean.DataEntity> getList() {
        return list;
    }
}
